/*
 * CustomerAction.java
 * 
 * Created on 02/10/2007, 12:43:14 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.actions;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.Estatuto;

import model.factory.RecreacionalFactory;
import model.service.EstatutoService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import view.common.struts.CustomActionForm;
import view.common.Constants;

import view.common.struts.CustomDispatchAction;

/**
 * @Archivo LoginAction.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:35:29 PM       moshiro
 */
public class EstatutoAction extends CustomDispatchAction {

    private final static String PRINCIPAL = "principal";
    private final static String REGISTRAR = "registrar";
    private final static String MODIFICAR = "modificar";
    private final static String LISTAR = "listar";

    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto beanSearch = new Estatuto();
        frm.setBeanSearch(beanSearch);
        frm.setBeanList(getEstatutoService().listarEstatuto(beanSearch));
        frm.setMapList(new HashMap());
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto beanSearch = (Estatuto) frm.getBeanSearch();
        frm.setBeanList(getEstatutoService().listarEstatuto(beanSearch));
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward refrescar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward registrar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto bean = new Estatuto();
        frm.setBean(bean);

        Estatuto filtro = new Estatuto();
        //frm.setReadOnly("N");
        frm.getMapList().put("estatutoList", getEstatutoService().listarEstatuto(filtro));
        return mapping.findForward(REGISTRAR);
    }

    public ActionForward eliminar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        getEstatutoService().eliminarEstatuto(Long.valueOf(frm.getSelectedIndex()));
        return unspecified(mapping, form, req, resp);
    }

    public ActionForward modificar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto beanSelected = new Estatuto(Long.valueOf(frm.getSelectedIndex()));
        Estatuto bean = getEstatutoService().obtenerEstatuto(beanSelected);
        frm.setBean(bean);

        Estatuto filtro = new Estatuto();
        //frm.setReadOnly("N");
        frm.getMapList().put("estatutoList", getEstatutoService().listarEstatuto(filtro));
        return mapping.findForward(MODIFICAR);
    }

    public ActionForward listar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto beanSelected = new Estatuto(Long.valueOf(frm.getSelectedIndex()));
        Estatuto bean = getEstatutoService().obtenerEstatuto(beanSelected);
        frm.setBean(bean);
        return mapping.findForward(LISTAR);
    }

    public ActionForward guardar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto bean = (Estatuto) frm.getBean();
        if (bean.getCodigo() == null) {
            //INSERTAR
            byte[] fileData = frm.getFormFile().getFileData();
            bean.setDocumento(fileData);
            frm.getFormFile().getInputStream().close();
            getEstatutoService().agregarEstatuto(bean);
        } else {
            //MODIFICAR
            getEstatutoService().modificarEstatuto(bean);
        }
        return unspecified(mapping, form, req, resp);
    }

    public ActionForward mostrar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Estatuto beanSelected = new Estatuto(Long.valueOf(frm.getSelectedIndex()));
        Estatuto bean = getEstatutoService().obtenerEstatuto(beanSelected);

        byte[] bytes = bean.getDocumento();
        response.setHeader("Content-Disposition", "attachment;filename=" + bean.getNombreDocumento());
        response.setContentType("application/pdf");
        response.getOutputStream().write(bytes);
        response.getOutputStream().flush();
        response.getOutputStream().close();
        return null;
    }

    public EstatutoService getEstatutoService() {
        return RecreacionalFactory.getInstance().getEstatutoService();
    }
}
